package com.ohs.fo.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.ohs.bll.util.ConfigurationManager;

public class SMSGateway extends AbstractAction {
	public static Map<String, String> verifyCodes = new HashMap<String, String>();
	private Log log = LogFactory.getLog(SMSGateway.class);
    private static final String allowIPs = "118.69.199.9 112.78.7.18 202.158.244.73 203.171.30.222";

    public String process() throws IOException{
    	HttpServletRequest request = ServletActionContext.getRequest();
    	HttpServletResponse response = ServletActionContext.getResponse();
        String ipAddress = request.getHeader("X-FORWARDED-FOR");  
        if (ipAddress == null) {  
            ipAddress = request.getRemoteAddr();  
        }
        boolean dev = ConfigurationManager.getAsBoolean(ConfigurationManager.CONFIG, "dev");
        if (dev || (!dev && allowIPs.contains(ipAddress))) {
            String message = (String) request.getParameter("message");
            if (message != null) {
                String phone = (String) request.getParameter("phone");
                String service = (String) request.getParameter("service");
                String captcha = RandomStringUtils.randomAlphanumeric(4);
                response.setContentType("application/xml");
                StringBuffer output = new StringBuffer();
                output.append("<ClientResponse>");
                output.append("<Message>");
                output.append("<PhoneNumber>" + phone + "</PhoneNumber>");
                output.append("<Message>Cam on ban da su dung dich vu. Ma xac nhan cua ban la: " + captcha + "</Message>");
                output.append("<SMSID>-1</SMSID>");
                output.append("<ServiceNo>" + service + "</ServiceNo>");
                output.append("</Message>");
                output.append("</ClientResponse>");
                PrintWriter writer = response.getWriter();
                writer.write(output.toString());
                
                //put captcha & key to global
                phone = phone.substring(phone.length() - 9, phone.length());
                verifyCodes.put(phone, captcha);
            } else {
            	log.error("Null SMS message");
            }
        } else {
        	log.error("SMS wrong IP:" + ipAddress );
        }
        return null;
    }
    
    public String ReplaceSpecialCharsForXML(String strString)
    {
        String strRet = "";
        if (strString != null)
        {
            strRet = strString.replace("\"", "&quot;");
            strRet = strRet.replace("&", "&amp;");
            strRet = strRet.replace("’", "&apos;");
            strRet = strRet.replace("'", "&apos;");
            strRet = strRet.replace("<", "&lt;");
            strRet = strRet.replace(">", "&gt;");
        }
        return strRet;
    }
}
